<template>
  <el-dialog
    title="发票管理"
    :close-on-click-modal="false"
    :visible.sync="visible" width="850px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-form-item label="合同编号" prop="contractNo">
        <el-input v-model="dataForm.contractNo" placeholder="合同编号" disabled></el-input>
      </el-form-item>
      <el-form-item label="合同名称" prop="contractName">
        <template slot-scope="scope">
          <strong>{{ dataForm.contractName }}</strong>
        </template>
      </el-form-item>
      <el-form-item label="发票记录">
        <template slot-scope="scope">
          <el-table :data="dataForm.billList" border>
            <el-table-column prop="createTime" label="时间" sortable width="180"></el-table-column>
            <el-table-column prop="createTime" label="是否开发票" sortable width="140">
              <template slot-scope="scope">
                <el-tag type="primary" v-if="scope.row.billSet === 1">是</el-tag>
                <el-tag type="info" v-else>否</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注"></el-table-column>
            <el-table-column label="操作" width="130">
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" circle @click="editBillHandle(scope.row.id)"></el-button>
                <el-button type="danger" icon="el-icon-delete" circle @click="deleteBillHandle(scope.row.id)"></el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: center">
      <el-button type="primary" @click="addBillHandle()">新增</el-button>
      <el-button @click="visible = false">返回</el-button>
    </div>

    <!-- 添加发票管理 对话框-->
    <el-dialog :title="(!dataForm.id ? '新增' : '修改' ) + '发票数据' " :close-on-click-modal="false" :visible.sync="billAddVisible" append-to-body>
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
        <el-form-item label="是否开发票" prop="billSet">
          <el-radio-group v-model="dataForm.billSet">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark" placeholder="备注" type="textarea" rows="4"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker v-model="dataForm.createTime" type="date" value-format="yyyy-MM-dd" placeholder="添加时间"
                          style="width: 200px;"></el-date-picker>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="billAddVisible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
  </el-dialog>
</template>

<script>
import {stringIsNull} from '@/utils'

export default {
  data () {
    return {
      visible: false,
      billAddVisible: false,
      dataForm: {
        id: 0,
        contractNo: '',
        contractName: '',
        billSet: '',
        remark: '',
        createTime: '',
        billList: [],
      },
      dataRule: {
        contractNo: [
          {required: true, message: '合同编号不能为空', trigger: 'blur'}
        ],
        billSet: [
          {required: true, message: '是否开发票不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init (item) {
      // this.dataForm.id = id || 0

      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.dataForm.contractNo = item.contractNo
        this.dataForm.contractName = item.contractName
        this.refreshDataList()
      })
    },
    // 刷新数据表
    refreshDataList () {
      this.billAddVisible = false
      this.getDataList(this.dataForm.contractNo).then(list => {
        this.dataForm.billList = list
      })
    },
    // 获取发票记录表
    getDataList (contractNo) {
      return new Promise((resolve, reject) => {
        this.$http({
          url: this.$http.adornUrl(`/project/contractbill/list`),
          method: 'get',
          params: this.$http.adornParams({
            'contractNo': contractNo
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            resolve(data.list)
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/project/contractbill/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'contractNo': this.dataForm.contractNo,
              'billSet': this.dataForm.billSet,
              'remark': this.dataForm.remark,
              'createTime': this.dataForm.createTime
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.refreshDataList()
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    // 新增发票记录
    addBillHandle () {
      this.billAddVisible = true
      this.dataForm.id = 0
      this.dataForm.billSet = ''
      this.dataForm.remark = ''
      this.dataForm.createTime = new Date()
    },
    // 编辑发票记录
    editBillHandle (id) {
      this.billAddVisible = true
      this.$http({
        url: this.$http.adornUrl(`/project/contractbill/info/${id}`),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataForm.id = id
          this.dataForm.contractNo = data.contractBill.contractNo
          this.dataForm.billSet = data.contractBill.billSet
          this.dataForm.remark = data.contractBill.remark
          this.dataForm.createTime = data.contractBill.createTime
        }
      })
    },
    // 删除发票记录
    deleteBillHandle (id) {
      let tip = '是否删除该发票记录'
      this.$confirm(tip, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var ids = [id]
        this.$http({
          url: this.$http.adornUrl('/project/contractbill/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500
            })
            this.refreshDataList()
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
